import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:preload_page_view/preload_page_view.dart';
import 'package:series_play/comm/event/switch_tab_event.dart';
import 'package:series_play/comm/ext/context_ext.dart';
import 'package:series_play/pages/bingewatch/binge_watch_page.dart';
import 'package:series_play/pages/perks/perks_page.dart';

import '../../comm/base/base_state.dart';
import '../../generated/assets.dart';
import '../../widget/app_image.dart';
import '../foryou/for_you_page.dart';
import '../home/home_page.dart';
import '../mine/mine_page.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends BaseState<MainPage> {
  int _currentIndex = 0;
  final _pageController = PageController();
  final pages = <Widget>[
    HomePage(),
    ForYouPage(),
    PerksPage(),
    BingeWatchPage(),
    MinePage(),
  ];
  
  @override
  void initState() {
    super.initState();
    _setStatusBarWhite();
    addWorker(ever(appController.eventBus, (v){
      if(v.event is SwitchTabEvent){
        final event = v.event as SwitchTabEvent;
        setState(() {
          _currentIndex = event.index;
          _pageController.jumpToPage(event.index);
        });
      }
    }));
  }

  /// 设置状态栏白色字体，透明背景
  void _setStatusBarWhite() {
    SystemChrome.setSystemUIOverlayStyle(
      const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.light, // Android
        statusBarBrightness: Brightness.dark,      // iOS
      ),
    );
  }

  /// 恢复默认状态栏（黑色字体）
  void _restoreStatusBar() {
    SystemChrome.setSystemUIOverlayStyle(
      const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.dark,  // Android
        statusBarBrightness: Brightness.light,     // iOS
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.light, // Android
        statusBarBrightness: Brightness.dark,      // iOS
      ),
      child: super.build(context),
    );
  }

  @override
  Widget buildPage(BuildContext context) {
    return PageTransitionSwitcher(
      transitionBuilder:
          (
          Widget child,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          ) {
        return FadeThroughTransition(
          animation: animation,
          secondaryAnimation: secondaryAnimation,
          fillColor: Colors.transparent,
          child: child,
        );
      },
      child: PageView.builder(
        itemCount: pages.length,
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
        // preloadPagesCount: 5,
        itemBuilder: (context, index) {
          return pages[index];
        },
      ),
    );
  }

  @override
  Widget? bottomNavigationBar() {
    Widget bar = BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: _currentIndex,
      backgroundColor: Color(0xff23252C),
      selectedItemColor: Colors.white,
      unselectedItemColor: Color(0xffb1b1b1),
      unselectedLabelStyle: TextStyle(fontSize: 10.sp),
      selectedLabelStyle: TextStyle(fontSize: 10.sp),
      showUnselectedLabels: true,
      items: [
        bottomBarItemFactory(
          Assets.imagesIcTabHomeUnselect,
          Assets.imagesIcTabHomeSelect,
          context.tr.tabHome,
        ),
        bottomBarItemFactory(
          Assets.imagesIcTabForYouUnselect,
          Assets.imagesIcTabForYouSelect,
          context.tr.tabForYou,
        ),
        bottomBarItemFactory(
          Assets.imagesIcTabGift,
          Assets.imagesIcTabGift,
          context.tr.tabPerks,
        ),
        bottomBarItemFactory(
          Assets.imagesIcTabBingeWatchUnselect,
          Assets.imagesIcTabBingeWatchSelect,
          context.tr.tabBingeWatch,
        ),
        bottomBarItemFactory(
          Assets.imagesIcTabMineUnselect,
          Assets.imagesIcTabMineSelect,
          context.tr.tabMe,
        ),
      ],
      onTap: (index) {
        setState(() {
          _currentIndex = index;
          _pageController.jumpToPage(index);
        });
      },
    );
    return Theme(
      data: Theme.of(context).copyWith(
        splashFactory: NoSplash.splashFactory,
        highlightColor: Colors.transparent,
      ),
      child: bar,
    );
  }

  BottomNavigationBarItem bottomBarItemFactory(
    String unselectSrc,
    String selectSrc,
    String label,
  ) {
    return BottomNavigationBarItem(
      icon: AppImage(unselectSrc),
      activeIcon: AppImage(selectSrc),
      label: label,
    );
  }

  @override
  bool hideAppbar() => true;
}
